$(function(){
	newGoodsInfo()
	remarks()
	selectGoods()
});

var is = false;
var goodsList= {};
// 备注
function remarks(){
	var tbody = $('.goods-box')
	tbody.on('click','.btn-add-remarks',function(){
		if(!is) return false;
		
		var $self = $(this)
		var text = $.trim($self.text())
		var textContent = '<textarea class="f14 g6 dib vt w300 textarea-remarks" rows="5" placeholder="请输入备注信息，最多不超过200字">'
			if(text!=''){
				textContent+=text
			}
		textContent+='</textarea>'
		
		//页面层
		layer.confirm(textContent, {
			  btn: ['确定','取消'],
			  title:'备注说明'
			}, function(index){
				var html ='<i class="dib vm ion-edit f14 g3 mr5"></i>'
				var remarks = $.trim($('.textarea-remarks').val())
				
				if(remarks==''){
					$self.html('<i class="dib vm ion-android-add f20 g3"></i><input name="remark" type="hidden" value="">')
					return layer.close(index);
				}
				
				html+=remarks
				$self.html(html+'<input name="remark" type="hidden" value="'+remarks+'">')
				layer.close(index);
			}, function(index){
				layer.close(index);
			});
		
	})
	
}

function newGoodsInfo(){
	var tbody = $('.goods-box')
	tbody.on('click','.btn-add-goods',function(){
		var $tr = tbody.find('tr')
		var num = +$tr.length+1
		var html = template(num)
		$tr.eq(0).find('.btn-less-goods').show()
		tbody.append(html)
	})
	
	tbody.on('click','.btn-less-goods',function(){
		var $tr = tbody.find('tr')
		if($tr.length==1) return false;
		if($tr.length==2){
			 $tr.eq(0).find('.btn-less-goods').hide()
		}
		var goodsId = $(this).parents('tr').find("input[name=goodsId]").val();
		if(goodsId != null){
			delete goodsList[goodsId];
		}
		$(this).parents('tr').remove();
		for(var i=1;i<=$tr.length;i++){
			$($tr[i]).find('.serial').html(i)
		}
		
	})

}

// 选择商品Id
function selectGoods(){
	var tbody = $('.goods-box')
	tbody.on('click','.btn-select-goods',function(){
		btnSelectGoods($(this))
		
		is = true;
		
	})
	
}

function btnSelectGoods($self){
	 top.jzts();
	 var basePath = $('base').attr('href')
	 var depotId=$("#depotId").val();
	 var diag = new top.Dialog();
	 diag.Drag=true;
	 diag.Title ="选择产品";
	 diag.URL = basePath+'stock/selectGoodsList.do?depotId='+depotId;
	 diag.Width = 700;
	 diag.Height = 419;
	 
	 diag.CancelEvent = function(){ //关闭事件
		 if(diag.innerFrame.contentWindow.document.getElementById('zhongxin').style.display == 'none'){
//			 if('${page.currentPage}' == '0'){
//				 top.jzts();
//				 setTimeout("self.location=self.location",100);
//			 }else{
//				 
//			 }
		}
		diag.close();
	 };
	 // 点击确认 选择
	 diag.OKEvent = function() {
		 
		 
	
		var child = diag.innerFrame.contentWindow.document;
		var goodsId= $(child).find("#ids").val();
		if(goodsList[goodsId] != null){
			alert('商品已存在');
			return;
		}
		
		var code=$(child).find("#code").val();
		var state=$(child).find("#state").val();
		var title=$(child).find("#title").val();
		var unit=$(child).find("#unit").val();
		var realStockNum=$(child).find("#realStockNum").val();
		var $unit = $.trim($('#unitList').val())
		var dataUnitVal = $.parseJSON($unit) 
		var selectEle = $self.parents('tr').find('select')
		var unitHtml = '';
			goodsList[goodsId] = goodsId;
			console.log(goodsList);
		if(unit=="t"){
			unitHtml+= '<option value=t>吨</option>' 
		}else if(unit =="kg"){
			unitHtml+= '<option value=kg>千克</option>' 
		}else if(unit =="box"){
			unitHtml+= '<option value=box>箱</option>' 
		}
		
		selectEle.attr('name','unit')
		selectEle.html(unitHtml)
		
		// 注：一定要放在最下面
		$self.parent().after('<td class="goods-name tc">'+title+'<input type="hidden" name="goodsId" value="'+goodsId+'"><input type="hidden" name="goodsName" value="'+title+'"></td>')
		$self.parent().html(code).removeAttr('colspan');

		 diag.close();
		 
	 }
	 diag.show();
}



	


function template(num){
	var html ='<tr><td class="tc serial">'+num+'</td><td><div class="tc">'+
	'<a class="dib vm ion-android-add btn-add-goods f20 g3" href="javascript:;"></a><a class="dib vm ion-android-remove btn-less-goods f20 g3 ml10" href="javascript:;"></a>'+
    '</div></td><td class="tc" colspan="2"><a class="btn-select-goods" href="javascript:;">选择商品</a></td><td class="tc"><select><option>请选择...</option></select></td>'+
    '<td class="tc"><input type="number" name="number" min="1" value="1" max="1000000000"></td><td class="tc"><div class="remarks-tips"><a class="btn-add-remarks"  href="javascript:;"><i class="dib vm ion-android-add f20 g3"></i><input name="remark" type="hidden" value=""></a></div></td></tr>';
	return html;
}